WWL.fileupload = function ( name )
{
	var up = new WWL ( 'fileupload', name );

	if ( typeof ( FormData ) == 'undefined' )
	{
		console.error ( 'XMLHttpRequest level 2 not found!' );
		return null;
	}
	up.url = '/ajax.pyhp';
	up.method = "POST";
	up.params = {};
	up._files = [];
	up.cbacks = {
		'progress': null,
		'load': null,
		'error': null
	};

	up.from_input = function ( input )
	{
		if ( ! input.type || input.type != 'file' ) return;
		for ( var i = 0; i < input._files.length; i++ )
		{
			up._files.push ( input._files.item ( i ) );
		}
	};

	up.add_file = function ( file )
	{
		if ( ! file || ! file.name || ! file.type || ! file.size ) return;
		up._files.push ( file );
	};

	up.post = function ()
	{
		var fd = new FormData ();
		for ( var k in up.params )
		{
			var v = up.params [ k ];
			fd.append ( k, v );
		}
		for ( var i in up._files )
		{
			var f = up._files [ i ];
			fd.append ( 'file' + i, f );
		}
		var xhr = new XMLHttpRequest ();
		var upload = xhr.upload;
		upload.addEventListener ( 'progress', up._onprogress, false );
		upload.addEventListener ( 'load', up._onload, false );
		upload.addEventListener ( 'error', up._onerror, false );
		xhr.open ( up.method, up.url );
		xhr.send ( fd );
	};

	up.reset = function ()
	{
		up._files = [];
	};

	up.debug = function ()
	{
		for ( var i in up._files )
		{
			var f = up._files [ i ];
			console.log ( "File %d : %s %s %s", i, f.name, f.type, f.size );
		}
	};

	up._onprogress = function ( e )
	{
		console.info ( 'PROGRESS: %o', arguments );
		if ( up.cbacks [ 'progress' ] ) up.cbacks [ 'progress' ] ();
	};

	up._onload = function ( e )
	{
		console.info ( 'LOAD: %o', arguments );
		if ( up.cbacks [ 'load' ] ) up.cbacks [ 'load' ] ();
	};

	up._onerror = function ( e )
	{
		console.error ( e );
		if ( up.cbacks [ 'error' ] ) up.cbacks [ 'error' ] ();
	};

	return up;
};
